<?php

/**
 * 登陆控制器
 * @author:Cloud <190296465@qq.com>
 * @copyright:TcitCMS
 * @since:2014-05-12
 */

namespace Admin\Controller;

class LoginController extends CommonController
{

    /**
     * 登录主页面
     * @author Cloud
     * @since 2014-05-12
     * @copyright TcitCMS
     */
    function index()
    {
        if (cookie(C('COOKIE_ID_ADMIN')))
            $this->success('欢迎回来！', '/');
        $this->display();
    }

    /**
     * 登陆
     */
    function LoginAction()
    {
        $username = I('post.username');
        $password = I('post.password');
        $remember = I('post.remember');
        $authcode = I('post.vcode');
        $model = new \Common\Model\UserModel();
        $res = $model->LoginAction($username, $password, $remember, $authcode);
        if ($res === true) {
            $this->success('登录成功！');
        } else {
            $this->error($res);
        }
    }

    /**
     * 退出登陆
     */
    function loginOut()
    {
        $model = new \Common\Model\UserModel();
        $model->loginOut();
        $this->success('退出成功', U('Index/index'));
    }

    function vcode()
    {
        $image = imagecreatetruecolor(105, 38);
        $bgcolor = imagecolorallocate($image, 255, 255, 255); //画笔着色
        $font = './Public/Common/fonts/msyhbd.ttc';
        imagefill($image, 0, 0, $bgcolor); //将底图的着色
        $vcode = "";
        for ($i = 0; $i < 4; $i++) {
            $fontsize = 20;
            $fontcolor = imagecolorallocate($image, rand(20, 100), rand(30, 100), rand(10, 200));
            $str = "abcdefghkmnpwsert1234567890"; //给出一个字符串，用于生成随机验证码
            $fontcontent = substr($str, rand(0, strlen($str)), 1); //每次截取一个字符
            $vcode.=$fontcontent; //拼接
            $x = ($i * 100 / 4) + rand(5, 10);
            $y = rand(25, 30);
            //imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
            imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $fontcontent);
        }
        $_SESSION['authcode'] = $vcode; //保存在session中
        //添加干扰点
        for ($i = 0; $i < 100; $i++) {
            $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
            imagesetpixel($image, rand(0, 100), rand(0, 30), $pointcolor);
        }
        //添加干扰线
        for ($i = 0; $i < 3; $i++) {
            $linecolor = imagecolorallocate($image, rand(80, 200), rand(80, 200), rand(80, 200));
            imageline($image, rand(5, 95), rand(5, 25), rand(5, 95), rand(5, 25), $linecolor);
        }
        ob_clean(); //清空缓存
        header("content-type:" . 'image/png'); //输出图片的头文件固定格式（可以是image/jpg、image/png、）
        imagepng($image); //向浏览器输出一张图片
        imagedestroy($image); //销毁内存中的底图
    }

}

?>
